package cn.lf.utils;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtil {

    public static String doGet(String url, Map<String,String> param){
        String resultString = "";
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse = null;
        try {
            URIBuilder uriBuilder = new URIBuilder();
            if(param!=null){
                for (String key : param.keySet()){
                    uriBuilder.addParameter(key,param.get(key));
                }
            }
            URI uri = uriBuilder.build();

            HttpGet httpGet = new HttpGet(uri);

            closeableHttpResponse = closeableHttpClient.execute(httpGet);
            if (closeableHttpResponse.getStatusLine().getStatusCode() == 200){
                resultString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(closeableHttpResponse != null){
                    closeableHttpResponse.close();
                }
                closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return resultString;
    }

    public static String doGet(String url){
        return doGet(url,null);
    }

    public static String doPost(String url,Map<String,String> param){
        String resultString = "";
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            //创建参数列表
            if(param!=null){
                List<NameValuePair> nameValuePairs = new ArrayList<>();
                for (String key : param.keySet()){
                    nameValuePairs.add(new BasicNameValuePair(key,param.get(key)));
                }
                //模拟表单
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairs);
                httpPost.setEntity(urlEncodedFormEntity);
            }
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            resultString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(closeableHttpResponse != null){
                    closeableHttpResponse.close();
                }
                closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return resultString;
    }

    public static String doPost(String url){
        return doPost(url,null);
    }

    public static String doPostJson(String url,String json){
        String resultString = "";
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse = null;
        try {
            HttpPost httpPost = new HttpPost(url);
            //创建请求内容
            StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(stringEntity);

            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            resultString = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(closeableHttpResponse != null){
                    closeableHttpResponse.close();
                }
                closeableHttpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return resultString;
    }

}
